package com.Utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pojo.FlightRecordPojo;
import com.pojo.OverallMonitoringPojo;

import java.sql.SQLOutput;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @version:java version 1.8
 * @Author:gzt
 * @description:
 * @date:2023-07-2712:26
 */
public class excelReader {

    public static void main(String[] args) {
        String filePath1 = "F:\\desktop\\filePath1.xlsx";
        String filePath2 = "F:\\desktop\\filePath2.xlsx";

        List<OverallMonitoringPojo> overallMonitoringPojoList = new ArrayList<>();
        EasyExcel.read(filePath1).head(OverallMonitoringPojo.class).sheet().registerReadListener(new AnalysisEventListener<OverallMonitoringPojo>() {
            @Override
            public void invoke(OverallMonitoringPojo overallMonitoringPojo, AnalysisContext analysisContext) {
                overallMonitoringPojoList.add(overallMonitoringPojo);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("1数据读取完毕");
            }
        }).doRead();

        List<FlightRecordPojo>  flightRecordPojos = new ArrayList<>();
        EasyExcel.read(filePath2).head(FlightRecordPojo.class).sheet().registerReadListener(new AnalysisEventListener<FlightRecordPojo>() {
            @Override
            public void invoke(FlightRecordPojo flightRecordPojo, AnalysisContext analysisContext) {
                flightRecordPojos.add(flightRecordPojo);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("2数据读取完毕");
            }
        }).doRead();

        Map<String, OverallMonitoringPojo> overallMonitoringPojoMap = new TreeMap<>();
        for (OverallMonitoringPojo o : overallMonitoringPojoList) {
            if (o.getStartTime() != null && o.getCloseTime() != null) {
                String s = o.getDay() + "/" + o.getMonth() + "/" + o.getYear();
                String key = s + o.getJiZhu() + o.getJiHao() + o.getJiXing() + o.getStartTime() + o.getCloseTime();
                overallMonitoringPojoMap.put(key, o);
            }
        }

        Map<String, FlightRecordPojo> flightRecordPojoMap = new TreeMap<>();
        for (FlightRecordPojo f : flightRecordPojos) {
            if (f.getStartTime() != null && f.getCloseTime() != null) {
                String key = f.getDate() + f.getJiZhu() + f.getJiHao() + f.getJiXing() + f.getStartTime() + f.getCloseTime();
                flightRecordPojoMap.put(key, f);
            }
        }

//        Iterator<Map.Entry<String, OverallMonitoringPojo>> iterator = overallMonitoringPojoMap.entrySet().iterator();
//        List<OverallMonitoringPojo> list = new ArrayList<>();
//        while (iterator.hasNext()) {
//            Map.Entry<String, OverallMonitoringPojo> next = iterator.next();
//            if (flightRecordPojoMap.containsKey(next.getKey())) {
//                OverallMonitoringPojo value = next.getValue();
//                FlightRecordPojo flightRecordPojo = flightRecordPojoMap.get(next.getKey());
//                value.setFlightTime(flightRecordPojo.getFlightTime());
//                value.setFlightTimeEnd(flightRecordPojo.getFlightTimeEnd());
//                list.add(value);
//            }
//        }
        for (OverallMonitoringPojo o : overallMonitoringPojoList) {
            String s = o.getDay() + "/" + o.getMonth() + "/" + o.getYear();
            String key = s + o.getJiZhu() + o.getJiHao() + o.getJiXing() + o.getStartTime() + o.getCloseTime();
            if (flightRecordPojoMap.containsKey(key)) {
                FlightRecordPojo flightRecordPojo = flightRecordPojoMap.get(key);
                o.setFlightTime(flightRecordPojo.getFlightTime());
                o.setFlightTimeEnd(flightRecordPojo.getFlightTimeEnd());
            }
        }

//        System.out.println(list.size());
        String filePath3 = "F:\\desktop\\filePath3.xlsx";
        EasyExcel.write(filePath3, OverallMonitoringPojo.class).sheet().doWrite(overallMonitoringPojoList);
        System.out.println("写入完成");
    }
}
